/* Copyright (c) [2024] [ylc]. Licensed under Apache-2.0 License. */
#include "HT1622.h"
#include "dis_7seg.h"

mHTTypedef_t Ht1={0};
HT_SEG_t HT_SEG;

/*********************************************************/
#define HT1622_DAT 		GPIO_Pin_8	//A
#define HT1622_WR  		GPIO_Pin_9	//A
#define HT1622_CS  		GPIO_Pin_9	//B

static void IoInit(void) {
	GPIOA_ModeCfg( HT1622_DAT, GPIO_ModeOut_PP_5mA );//
	GPIOA_ModeCfg( HT1622_WR, GPIO_ModeOut_PP_5mA ); //
	GPIOB_ModeCfg( HT1622_CS, GPIO_ModeOut_PP_5mA ); //	
}

static void DelayUs(void) {
    u16 i;
    for (i = 0;i < 10;i++) {
        _nop_();
    }
}

static void CsH(void) {
    GPIOB_SetBits(HT1622_CS);
}

static void CsL(void) {
    GPIOB_ResetBits(HT1622_CS);
}

static void WrH(void) {
    GPIOA_SetBits(HT1622_WR);
}

static void WrL(void) {
    GPIOA_ResetBits(HT1622_WR);
}

static void DatH(void) {
    GPIOA_SetBits(HT1622_DAT);
}

static void DatL(void) {
    GPIOA_ResetBits(HT1622_DAT);
}

/*********************************************************/
void HT_SendNBit(mHTTypedef_t* ht,u8 sdata,u8 cnt){
	u8 i;
	for(i=0;i<cnt;i++){
		if(sdata&0x80)
			ht->DAT_H();
		else
			ht->DAT_L();
		ht->WR_L();
		ht->DelayUs();
		ht->WR_H();
		ht->DelayUs();
		sdata<<=1;
	}
}

void HT_WrCmd(mHTTypedef_t* ht,u8 ucTmpCmd){
	ht->CS_L();
	ht->DelayUs();
	HT_SendNBit(ht,0x80,3);
	HT_SendNBit(ht,ucTmpCmd,8);
	HT_SendNBit(ht,0x00,1);	
	ht->CS_H();
	//写入标志'100'+8bit cmd +1bit X
}

void HT_WrDat(mHTTypedef_t* ht,u8 addr,u8 dat){
	ht->CS_L();
	ht->DelayUs();
	HT_SendNBit(ht,0xa0,3);
	HT_SendNBit(ht,addr<<2,6);
	HT_SendNBit(ht,dat,4);
	ht->CS_H();
}

void HT_WrNDat(mHTTypedef_t* ht,u8 addr,u8 *dat,u8 cnt){
	u8 i=0;
	ht->CS_L();
	ht->DelayUs();
	HT_SendNBit(ht,0xa0,3);
	HT_SendNBit(ht,addr<<2,6);
	for(i=0;i<cnt;i++){
		HT_SendNBit(ht,dat[i],4);
	}
	ht->CS_H();
}

//设备注册
void ht_device_init(mHTTypedef_t* ht)
{
    ht->InitPort = IoInit;
    ht->DelayUs = DelayUs;
    ht->CS_H = CsH;
    ht->CS_L = CsL;
    ht->WR_H = WrH;
    ht->WR_L = WrL;
    ht->DAT_H = DatH;
    ht->DAT_L = DatL;
}

void ht1622_init(void){//HT1622功能初始化
    ht_device_init(&Ht1);
	Ht1.InitPort();

	Ht1.CS_H();
	Ht1.WR_H();
	Ht1.DAT_H();
	delay_ms(50);      //延时使LCD工作电压稳定
	HT_WrCmd(&Ht1,HT_RC32);   //使用内部振荡器
	HT_WrCmd(&Ht1,HT_SYSDIS);
	HT_WrCmd(&Ht1,HT_TIMDIS);
	HT_WrCmd(&Ht1,HT_WDTDIS2);
	HT_WrCmd(&Ht1,HT_SYSEN);
	HT_WrCmd(&Ht1,HT_LCDON);
	HT_WrCmd(&Ht1,HT_IRQDIS);
	delay_ms(50);
	ht1622_clear();
}

void ht1622_off(void){	
    HT_WrCmd(&Ht1,HT_SYSDIS);
}

void ht1622_on(void){
    HT_WrCmd(&Ht1,HT_SYSEN);
}

void ht1622_clear(void)
{
    memset((void *)HT_SEG.buf,0,HT_DAT_MAX);
    HT_WrNDat(&Ht1,0,(u8*)HT_SEG.buf,HT_DAT_MAX);
}

void ht1622_refresh(void)
{
    //段码对应
    HT_SEG.led.A1=DIS_SEG.led.A1;
    HT_SEG.led.B1=DIS_SEG.led.B1;
    HT_SEG.led.C1=DIS_SEG.led.C1;
    HT_SEG.led.D1=DIS_SEG.led.D1;
    HT_SEG.led.E1=DIS_SEG.led.E1;
    HT_SEG.led.F1=DIS_SEG.led.F1;
    HT_SEG.led.G1=DIS_SEG.led.G1;
    HT_SEG.led.H1=DIS_SEG.led.H1;

    HT_SEG.led.A2=DIS_SEG.led.A2;
    HT_SEG.led.B2=DIS_SEG.led.B2;
    HT_SEG.led.C2=DIS_SEG.led.C2;
    HT_SEG.led.D2=DIS_SEG.led.D2;
    HT_SEG.led.E2=DIS_SEG.led.E2;
    HT_SEG.led.F2=DIS_SEG.led.F2;
    HT_SEG.led.G2=DIS_SEG.led.G2;
    HT_SEG.led.H2=DIS_SEG.led.H2;

    HT_SEG.led.A3=DIS_SEG.led.A3;
    HT_SEG.led.B3=DIS_SEG.led.B3;
    HT_SEG.led.C3=DIS_SEG.led.C3;
    HT_SEG.led.D3=DIS_SEG.led.D3;
    HT_SEG.led.E3=DIS_SEG.led.E3;
    HT_SEG.led.F3=DIS_SEG.led.F3;
    HT_SEG.led.G3=DIS_SEG.led.G3;
    HT_SEG.led.H3=DIS_SEG.led.H3;

    HT_WrNDat(&Ht1,0,(u8*)HT_SEG.buf,HT_DAT_MAX);
}

